<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="title" value="物流管理"/>
<%@ include file="../common/header.jsp" %>

<div class="main-content">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">${title}</h1>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addLogisticsModal">
            <i class="fa-solid fa-plus-circle"></i> 添加物流记录
        </button>
    </div>
    <!-- 筛选表单 -->
    <form class="row g-3 mb-3" method="get" action="">
        <div class="col-md-2">
            <label class="form-label">物流单号</label>
            <input type="text" class="form-control" name="logisticsNo" value="${logisticsNo}">
        </div>
        <div class="col-md-2">
            <label class="form-label">批次编号</label>
            <input type="text" class="form-control" name="batchNo" value="${batchNo}">
        </div>
        <div class="col-md-2">
            <label class="form-label">目的地</label>
            <input type="text" class="form-control" name="destination" value="${destination}">
        </div>
        <div class="col-md-2">
            <label class="form-label">运输状态</label>
            <select class="form-select" name="status">
                <option value="">全部</option>
                <option value="运输中" <c:if test="${status == '运输中'}">selected</c:if>>运输中</option>
                <option value="已签收" <c:if test="${status == '已签收'}">selected</c:if>>已签收</option>
                <option value="异常" <c:if test="${status == '异常'}">selected</c:if>>异常</option>
            </select>
        </div>
        <div class="col-md-2">
            <label class="form-label">发货时间（起）</label>
            <input type="datetime-local" class="form-control" name="startTime" value="${startTime}">
        </div>
        <div class="col-md-2">
            <label class="form-label">发货时间（止）</label>
            <input type="datetime-local" class="form-control" name="endTime" value="${endTime}">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button type="submit" class="btn btn-primary me-2">筛选</button>
            <a href="${pageContext.request.contextPath}/processing/logistics_management" class="btn btn-secondary">重置</a>
        </div>
    </form>
    <!-- 数据列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">物流记录列表</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>物流单号</th>
                            <th>批次编号</th>
                            <th>目的地</th>
                            <th>运输状态</th>
                            <th>发货时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach var="item" items="${logisticsList}">
                            <tr>
                                <td>${item.logisticsNo}</td>
                                <td>${item.batchNo}</td>
                                <td>${item.destination}</td>
                                <td>${item.status}</td>
                                <td><fmt:formatDate value="${item.sendTime}" pattern="yyyy-MM-dd HH:mm"/></td>
                            </tr>
                        </c:forEach>
                        <c:if test="${empty logisticsList}">
                            <tr><td colspan="5" class="text-center">暂无数据</td></tr>
                        </c:if>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<!-- 添加物流记录模态框 -->
<div class="modal fade" id="addLogisticsModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">添加物流记录</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="addLogisticsForm">
                    <div class="mb-3">
                        <label class="form-label">物流单号</label>
                        <input type="text" class="form-control" name="logisticsNo" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">批次编号</label>
                        <input type="text" class="form-control" name="batchNo" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">目的地</label>
                        <input type="text" class="form-control" name="destination" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">运输状态</label>
                        <select class="form-select" name="status" required>
                            <option value="运输中">运输中</option>
                            <option value="已签收">已签收</option>
                            <option value="异常">异常</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">发货时间</label>
                        <input type="datetime-local" class="form-control" name="sendTime" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="submitAddLogistics()">保存</button>
            </div>
        </div>
    </div>
</div>
<script>
function submitAddLogistics() {
    var form = $('#addLogisticsForm');
    var time = form.find('input[name="sendTime"]').val().replace('T', ' ');
    if (time.length === 16) time += ':00'; // 自动补全秒
    var data = {
        logisticsNo: form.find('input[name="logisticsNo"]').val(),
        batchNo: form.find('input[name="batchNo"]').val(),
        destination: form.find('input[name="destination"]').val(),
        status: form.find('select[name="status"]').val(),
        sendTime: time
    };
    $.ajax({
        url: '${pageContext.request.contextPath}/processing/addLogistics',
        type: 'POST',
        data: JSON.stringify(data),
        contentType: 'application/json',
        success: function(res) {
            $('#addLogisticsModal').modal('hide');
            location.reload();
        },
        error: function() {
            alert('添加失败，请重试');
        }
    });
}
</script>
<%@ include file="../common/footer.jsp" %> 